<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/6
 * Time: 21:36
 */

namespace app\admin\controller;
use think\Controller;
use think\Db;
class Consult extends Common
{
    //商品咨询首页
    //$list 咨询表与商品表联合查询且用户名不等于admin的数据
    //neq ！=
    public function consultindex(){
        $list=Db('consult')
            ->alias('c')
            ->join('tp_goods g','c.goods_id=g.goods_id')
            ->where('username','NEQ',session('admin')['user_name'])
            ->paginate();
        $consult_type = array(0=>'默认咨询',1=>'商品咨询',2=>'支付咨询',3=>'配送',4=>'售后');
        $this->assign('consult_type',$consult_type);
        $this->assign('list',$list);
        $this->getConAndAct('Goods');
        return $this->fetch('ConsultIndex');
    }
    /*
    * 咨询详情页
    * goods_id 商品id
    * $reply 查询管理员回复的消息
    */
    public function ConsultDetail(){
        $goods_id=input('goods_id');
        $content=input('content');
        $list=Db('consult')->where('goods_id',$goods_id)->find();
        $parent_id=Db('consult')->where('goods_id',$goods_id)->value('id');
        if($content!=null){
            $data=[
                'goods_id'=>$goods_id,
                'content'=>$content,
                'parent_id'=>$parent_id,
                'add_time'=>time(),
                'username'=>session('admin')['user_name']
            ];
            Db('consult')->insert($data);
        }
        $reply=Db('consult')
            ->alias('c')
            ->join('tp_goods g','c.goods_id=g.goods_id')
            ->where('username','EQ',session('admin')['user_name'])
            ->select();
        $this->assign('list',$list);
        $this->assign('reply',$reply);
        return $this->fetch('ConsultDetail');
    }

//删除
    public function ConsultDelete(){
        $id=input('id');
        $res=Db('consult')->where('id',$id)->delete();
        if ($res){
            return retJson('',200,'删除成功');
        }else{
            return retJson('',201,'删除失败');
        }
    }

 //咨询搜索
    public function ConsultSearch(){
        $searchContent=input('searchContent');
        $searchUser=input('searchUser');
            $list=Db('consult')
                ->alias('c')
                ->where('content','like',"%$searchContent%")
                ->whereOr('username','like',"%$searchUser%")
                ->join('tp_goods g','c.goods_id=g.goods_id')
                ->where('username','NEQ',session('admin')['user_name'])
                ->paginate(null,false,['query' => request()->param(), ]);

        $consult_type = array(0=>'默认咨询',1=>'商品咨询',2=>'支付咨询',3=>'配送',4=>'售后');
        $this->assign('consult_type',$consult_type);
        $this->assign('list',$list);
        return $this->fetch('ConsultIndex');
    }
}